﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.Model;
using ProdeManagmente.DAO;
using DgvFilterPopup;
using ProdeManagmente.Libs;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmTeamsTournament : Form
    {
        private Team team;
        private TeamService teamService;
        private TournamenteService tournamentService;

        public FrmTeamsTournament(Team team)
        {
            InitializeComponent();
            this.team = team;
            loadData();
        }

        private void loadData()
        {
            loadTournament();
            loadTournamentTeam();
            lblNombre.Text = team.getName();
        }

        private void loadTournamentTeam()
        {
            List<Tournament> tournamentsList = new List<Tournament>();
            teamService = TeamService.getTeamServiceInstance();

            tournamentsList = teamService.getTournamentTeam(team.getId());


            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[4] {
                                new DataColumn("ID",typeof(int)),
                                new DataColumn("TORNEO",typeof(string)),
                                new DataColumn("INICIO",typeof(DateTime)),
                                new DataColumn("FINAL",typeof(DateTime))});

            foreach (Tournament tournament in tournamentsList)
            {
                dataTable.Rows.Add(tournament.getId(), tournament.getName(), tournament.getStart(), tournament.getEnd());
            }

            dataGridView2.DataSource = dataTable;
            DgvFilterManager filterManager = new DgvFilterManager(dataGridView2);
        }

        private void loadTournament()
        {
            List<Tournament> tournamentsList = new List<Tournament>();
            tournamentService = TournamenteService.getTournamenteServiceInstance();
            String SQL = "Finish > '" + DateTime.Today.ToString("o") + "' ";
            tournamentsList = tournamentService.getTournamentListFilter(SQL);


            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[4] {
                                new DataColumn("ID",typeof(int)),
                                new DataColumn("TORNEO",typeof(string)),
                                new DataColumn("INICIO",typeof(DateTime)),
                                new DataColumn("FINAL",typeof(DateTime))});

            foreach (Tournament tournament in tournamentsList)
            {
                dataTable.Rows.Add(tournament.getId(), tournament.getName(), tournament.getStart(), tournament.getEnd());
            }

            dataGridView1.DataSource = dataTable;
            DgvFilterManager filterManager = new DgvFilterManager(dataGridView1);
        }

        private void save()
        {
            tournamentService = TournamenteService.getTournamenteServiceInstance();
            teamService = TeamService.getTeamServiceInstance();
            Persistencia conn = Persistencia.getInstance();
            try
            {
                conn.abrirTransaccion();
                teamService.deleteTournamentTeam(team.getId());
                foreach (DataGridViewRow row in dataGridView2.Rows)
                {
                    long idTournament = Convert.ToInt64(row.Cells[0].Value.ToString());
                    tournamentService.saveTeamTournament(idTournament, team.getId());
                }
                conn.commit();
                lblInfo.Text = "Operacion exitosa";
            }
            catch (BusinessException e)
            {
                conn.rolback();
                new MensajeSalida(e).showMensaje();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow selectRow = dataGridView1.SelectedRows[0];

                if (!rowIsAssigned(selectRow))
                {
                    DataTable data = (DataTable)dataGridView2.DataSource;
                    DataRow newRow =  data.NewRow();
                    newRow[0] = selectRow.Cells[0].Value;
                    newRow[1] = selectRow.Cells[1].Value;
                    newRow[2] = selectRow.Cells[2].Value;
                    newRow[3] = selectRow.Cells[3].Value;
                    data.Rows.Add(newRow);

                    dataGridView2.DataSource = data;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(dataGridView2.SelectedRows.Count > 0)
            {
                DataGridViewRow selectRow = dataGridView2.SelectedRows[0];

                dataGridView2.Rows.Remove(selectRow);
            }
        }

        private bool rowIsAssigned(DataGridViewRow row)
        {
            long idTournament = Convert.ToInt64(row.Cells[0].Value.ToString());

            foreach (DataGridViewRow rowAssigned in dataGridView2.Rows)
            {
                if (Convert.ToInt64(rowAssigned.Cells[0].Value.ToString()) == idTournament)
                {
                    return true;
                }
            }

            return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
